package com.mstore.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.mstore.pojo.BaseMember;

public class BaseMemberDao extends SuperDao {

	public void save(BaseMember baseMember) {
		super.save(baseMember);
	}

	public void delete(BaseMember baseMember) {
		super.delete(baseMember);
	}

	/**
	 * 更新
	 */
	public void updateBaseMember(BaseMember baseMember) {
		String separate = " set ";
		
		StringBuilder sb = new StringBuilder();
		sb.append("update ubcenter.c_base_member");
		
		Double balance = baseMember.getBalance();
		if (balance != null) {
			sb.append(separate);
			sb.append("balance = ");
			sb.append(balance);
			separate = ", ";
		}
		
		String level = baseMember.getLevel();
		if (level != null) {
			sb.append(separate);
			sb.append("level = '");
			sb.append(level);
			separate = ", ";
			sb.append("'");
		}
		
		Double score = baseMember.getScore();
		if (score != null) {
			sb.append(separate);
			sb.append("score = ");
			sb.append(score);
		}
		
		sb.append(" where id = ");
		sb.append(baseMember.getId());
		
		super.executeUpdate(sb.toString());
	}
	
	/**
	 * 根据memberId和eaId更新
	 */
	public void updateBaseMember1(BaseMember baseMember) {
		String separate = " set ";
		
		StringBuilder sb = new StringBuilder();
		sb.append("update ubcenter.c_base_member");
		
		Double balance = baseMember.getBalance();
		if (balance != null) {
			sb.append(separate);
			sb.append("balance = ");
			sb.append(balance);
			separate = ", ";
		}
		
		String level = baseMember.getLevel();
		if (level != null) {
			sb.append(separate);
			sb.append("level = '");
			sb.append(level);
			separate = ", ";
			sb.append("'");
		}
		
		Double score = baseMember.getScore();
		if (score != null) {
			sb.append(separate);
			sb.append("score = ");
			sb.append(score);
		}
		
		sb.append(" where ea_id = ");
		sb.append(baseMember.getId());
		sb.append(" and member_id = ");
		sb.append(baseMember.getMemberId());
		
		super.executeUpdate(sb.toString());
	}
	
	/**
	 * 根据memberId和eaId更新余额
	 */
	public void updateBaseMemberBanlance(BaseMember baseMember) {
		String separate = " set ";
		
		StringBuilder sb = new StringBuilder();
		sb.append("update ubcenter.c_base_member");
		
		Double balance = baseMember.getBalance();
		if (balance != null) {
			sb.append(separate);
			sb.append("balance = balance + ");
			sb.append(balance);
			separate = ", ";
		}
		
		sb.append(" where ea_id = ");
		sb.append(baseMember.getEaId());
		sb.append(" and member_id = ");
		sb.append(baseMember.getMemberId());
		
		super.executeUpdate(sb.toString());
	}
	
	/**
	 * 根据memberId查询记录
	 */
	public List<Object> getBaseMembersByMemberId(Long memberId) {
		String sql = "from BaseMember where memberId = ?";
		return super.getObject(sql, new Object[] { memberId });
	}
	
	/**
	 * 根据eaId查询记录
	 */
	public List<Object> getBaseMembersByEaId(Long eaId) {
		String sql = "from BaseMember where eaId = ?";
		return super.getObject(sql, new Object[] { eaId });
	}
	
	/**
	 * 根据eaId查询记录
	 * 分页
	 */
	public List<Object> getBaseMembersByEaId(Long eaId, int index) {
		StringBuilder bu = new StringBuilder();
		bu.append("from BaseMember where eaId = ");
		bu.append(eaId);
		bu.append(" order by createTime desc");
		return super.findPage(index, bu.toString());
	}
	
	/**
	 * 查询企业会员总数
	 */
	public int countBaseMemberByEaId(Long eaId) {
		String sql = "SELECT COUNT(ac) FROM BaseMember ac where ac.eaId = " + eaId;
		List<Object> objects = super.find(sql);
		if (!objects.isEmpty()) {
			Long count = (Long) objects.get(0);
			return count.intValue();
		}
		return 0;
	}
	
	/**
	 * 根据memberId和eaId查询记录
	 */
	public Object getOneBaseMemberByMemberIdAndEaId(Long memberId, Long eaId) {
		String sql = "from BaseMember where memberId = ? and eaId = ?";
		List<Object> objects = super.getObject(sql,
				new Object[] { memberId, eaId });
		if (objects.isEmpty()) {
			return null;
		}
		return objects.get(0);
	}
	
	/**
	 * 查询某时间段内企业会员的数量
	 */
	public Long getCountBaseMemberByTime(Long eaId, String begin, String end) {
		String separate = " where ";
		StringBuilder sb = new StringBuilder();
		sb.append("select count(id) from BaseMember");
		if (eaId != null) {
			sb.append(separate);
			sb.append("eaId = ");
			sb.append(eaId);
			separate = " and ";
		}
		if (StringUtils.isNotBlank(begin) && StringUtils.isNotBlank(end)) {
			sb.append(separate);
			sb.append("(createTime between '");
			sb.append(begin);
			sb.append("' and '");
			sb.append(end);
			sb.append("')");
			separate = " and ";
		}
		
		List<Object> objs = find(sb.toString());
		if (objs.isEmpty()) {
			return 0L;
		}
		
		return (Long) objs.get(0);
	}
	
	/**
	 * 通过baseMemberId查询
	 */
	public Object getOneBaseMemberByBaseMemberId(Long baseMemberId) {
		String sql = "from BaseMember where id = ?";
		List<Object> objects = super.getObject(sql,
				new Object[] { baseMemberId });
		if (objects.isEmpty()) {
			return null;
		}
		return objects.get(0);
	}

}
